/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package module1;

import dao.ChiTietHoaDonDAO;
import dao.HoaDonDAO;
import dao.KhachHangDAO;
import java.util.ArrayList;
import java.util.Date;
import java.util.Vector;
import javax.swing.JInternalFrame;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import pojo.ChiTietHoaDon;
import pojo.HoaDon;
import pojo.KhachHang;
import pojo.SanPham;

/**
 *
 * @author OSIRIS
 */
public class LapHoaDonFrame extends javax.swing.JInternalFrame {

    /**
     * Creates new form LapHoaDonFrame
     */
    public LapHoaDonFrame() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jTextMaKhachHang = new javax.swing.JTextField();
        jTextTenKhachHang = new javax.swing.JTextField();
        btnAddKhachHang = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTableDsSanPham = new javax.swing.JTable();
        jBtnThemSamPham = new javax.swing.JButton();
        jBtnXoaSanPham = new javax.swing.JButton();
        jBtnLapHoaDon = new javax.swing.JButton();

        setClosable(true);

        jLabel1.setText("Mã Khách Hàng :");

        jLabel2.setText("Tên Khách Hàng");

        jTextMaKhachHang.setEditable(false);
        jTextMaKhachHang.setHorizontalAlignment(javax.swing.JTextField.TRAILING);

        jTextTenKhachHang.setEditable(false);

        btnAddKhachHang.setText("Add Khách Hàng");
        btnAddKhachHang.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddKhachHangActionPerformed(evt);
            }
        });

        jLabel3.setText("Danh Sách Sản Phẩm");

        jTableDsSanPham.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        jScrollPane1.setViewportView(jTableDsSanPham);

        jBtnThemSamPham.setText("Thêm");
        jBtnThemSamPham.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnThemSamPhamActionPerformed(evt);
            }
        });

        jBtnXoaSanPham.setText("Xóa");
        jBtnXoaSanPham.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnXoaSanPhamActionPerformed(evt);
            }
        });

        jBtnLapHoaDon.setText("Lập Hóa Đơn");
        jBtnLapHoaDon.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnLapHoaDonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(46, 46, 46)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2))
                        .addGap(33, 33, 33)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jTextTenKhachHang, javax.swing.GroupLayout.DEFAULT_SIZE, 119, Short.MAX_VALUE)
                            .addComponent(jTextMaKhachHang))
                        .addGap(94, 94, 94)
                        .addComponent(btnAddKhachHang))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(34, 34, 34)
                        .addComponent(jLabel3))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(24, 24, 24)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 645, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(26, 26, 26)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jBtnThemSamPham, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jBtnXoaSanPham, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(276, 276, 276)
                        .addComponent(jBtnLapHoaDon)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(31, 31, 31)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1)
                            .addComponent(jTextMaKhachHang, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(btnAddKhachHang))
                        .addGap(34, 34, 34)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(jTextTenKhachHang, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(40, 40, 40)
                        .addComponent(jLabel3)
                        .addGap(18, 18, 18)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 176, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(216, 216, 216)
                        .addComponent(jBtnThemSamPham)
                        .addGap(18, 18, 18)
                        .addComponent(jBtnXoaSanPham)))
                .addGap(28, 28, 28)
                .addComponent(jBtnLapHoaDon)
                .addContainerGap(40, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnAddKhachHangActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddKhachHangActionPerformed
        // TODO add your handling code here:

        JInternalFrame dlg = new KhachHangFrame(this, "addHD");

        this.getDesktopPane().add(dlg);
        // this.add(dlg); 
        dlg.setVisible(true);



    }//GEN-LAST:event_btnAddKhachHangActionPerformed
    DefaultTableModel dtm;
    private void jBtnThemSamPhamActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnThemSamPhamActionPerformed
        // TODO add your handling code here:
        ThemSanPhamHoaDonDialog dlg = new ThemSanPhamHoaDonDialog(null, true);
        dlg.setVisible(true);
        if (dtm == null) {
            dtm = new DefaultTableModel();
            dtm.addColumn("ID");
            dtm.addColumn("Sku");
            dtm.addColumn("Ma Danh Muc");
            dtm.addColumn("Ten San Pham");
            dtm.addColumn("Kick Co");
            dtm.addColumn("Mau Sac");
            dtm.addColumn("So Luong");
            dtm.addColumn("Thành Tiền");

        }
        SanPham info = dlg.getInfo();
        Vector vt = new Vector();
        vt.add(info.getId());
        vt.add(info.getSku());
        vt.add(info.getDanhMuc().getId());
        vt.add(info.getTenSanPham());
        vt.add(info.getKickCo());
        vt.add(info.getMauSac());
        vt.add(dlg.getSoluong());
        vt.add(dlg.getSoluong() * info.getGiaBan());
        dtm.addRow(vt);
        this.jTableDsSanPham.setModel(dtm);
    }//GEN-LAST:event_jBtnThemSamPhamActionPerformed

    private void jBtnXoaSanPhamActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnXoaSanPhamActionPerformed
        // TODO add your handling code here:
        int selectedRow = jTableDsSanPham.getSelectedRow();
        dtm.removeRow(selectedRow);



    }//GEN-LAST:event_jBtnXoaSanPhamActionPerformed

    private void jBtnLapHoaDonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnLapHoaDonActionPerformed
        // TODO add your handling code here:
        try {
            HoaDonDAO hdDao = new HoaDonDAO();
            HoaDon hd = new HoaDon();
            Double thanh_tien = 0.0;
            int khach_hang_id = Integer.parseInt(jTextMaKhachHang.getText());
            int n = dtm.getRowCount();
            int m = dtm.getColumnCount();
            hd.setThanhTien(thanh_tien);
            KhachHangDAO khDao = new KhachHangDAO();
            KhachHang khach_hang = khDao.find(khach_hang_id);
            hd.setKhachHang(khach_hang);
            Date date = new Date();
            hd.setNgayLap(date);
            hdDao.create(hd);
            //ArrayList<Integer> dsSanpham = new ArrayList<Integer>();
                    ChiTietHoaDonDAO cthdDao=new ChiTietHoaDonDAO();
            for (int i = 0; i < n; i++) {
               // for (int j = 0; j < m; j++) {
                Integer id=Integer.parseInt(jTableDsSanPham.getValueAt(i, 0).toString());
                Double tt=Double.parseDouble(jTableDsSanPham.getValueAt(i, m-1).toString());
                Integer soluong=Integer.parseInt(jTableDsSanPham.getValueAt(i, m-2).toString());
                 //   dsSanpham.add(id);
                    thanh_tien += tt;
            
            
                ChiTietHoaDon cthd=new ChiTietHoaDon();
                cthd.setThanhTien(tt);
                cthd.setHoaDon(hd);
                cthd.setSanPhamId(id);
                cthd.setSoLuong(soluong);
                cthdDao.create(cthd);
            
               // }

            }
            hd.setThanhTien(thanh_tien);
            hdDao.update(hd);
            
            
                JOptionPane.showMessageDialog(this, "success");
                
            

        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "error");
        }


    }//GEN-LAST:event_jBtnLapHoaDonActionPerformed

    public void AddKhachHang(KhachHang khach_hang) {
        if (khach_hang != null) {
            jTextMaKhachHang.setText(String.valueOf(khach_hang.getId()));
            jTextTenKhachHang.setText(khach_hang.getHoTen());
        }
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAddKhachHang;
    private javax.swing.JButton jBtnLapHoaDon;
    private javax.swing.JButton jBtnThemSamPham;
    private javax.swing.JButton jBtnXoaSanPham;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTableDsSanPham;
    private javax.swing.JTextField jTextMaKhachHang;
    private javax.swing.JTextField jTextTenKhachHang;
    // End of variables declaration//GEN-END:variables
}
